package com.beidouapp.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ws.security.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beidouapp.domain.pojo.ChinaData;
import com.beidouapp.domain.pojo.Engineer;
import com.beidouapp.domain.pojo.EngineerPos;
import com.beidouapp.domain.pojo.StatisData;
import com.beidouapp.domain.vo.OrgRoleEmployeeVO;
import com.beidouapp.exception.BusinessException;
import com.beidouapp.manager.EngineerManager;
import com.beidouapp.service.EngineerService;

@Service
public class EngineerServiceImpl implements EngineerService {
	@Autowired
	EngineerManager engineerManager;
	
	@Override
	public List<EngineerPos> getEngineerPosList() {
		 
		return engineerManager.getEngineerPosList();
	}

	@Override
	public Map<String, Object> getDistributeTotal() {
		Map<String, Object> dMap= engineerManager.getDistributeTotal();
		if(null==dMap){
			dMap=new HashMap<String, Object>();
			dMap.put("ENGINEER_TOTAL", 0);
			dMap.put("PROJECT_TOTAL", 0);
		}
		return dMap;
	}

	@Override
	public List<Engineer> findEngineersByMap(Map<String, Object> params,
			Integer offset, Integer limit) {
		 List<Engineer> list = engineerManager.findEngineersByMap(params, offset, limit);
		return list;
	}

	@Override
	public Integer findEngineersCountByMap(Map<String, Object> params) {
		 
		return engineerManager.findEngineersCountByMap(params);
	}

	@Override
	public void saveOrUpdateEngineer(Engineer engineer) {
		if(null==engineer){
			throw new BusinessException("更新对象不能为null");	
		}
		if(StringUtils.isBlank(engineer.getId())){
			engineer.setSource(Engineer.SourceType.Consult.getCode());
			engineer.setRegDate(String.valueOf(System.currentTimeMillis()));
			Engineer target=engineerManager.getUserNameByEngineer(engineer.getUsername());
			if(target!=null){
				throw new BusinessException("用户名"+target.getUsername()+"在系统中重复");
			}
			engineerManager.save(engineer);
		}else{
			engineerManager.update(engineer);
		}
		
		
	}

	@Override
	public Engineer getEngineerById(Integer id) {
		 if(null==id){
			 throw new BusinessException("当前对象不存在");
		 }
	Engineer engineer=	engineerManager.getEngineerById(id);
	engineer=engineer==null?new Engineer():engineer;
	return engineer;
	}

	@Override
	public Map<String, Object> getEngineerDetailById(Integer id) {
		 if(null==id){
			 throw new BusinessException("当前对象不存在！");
		 }
		return engineerManager.getEngineerDetailById(id);
	}

	@Override
	public List<StatisData> findStatisDataListByMap(
			Map<String, Object> params, Integer offset, Integer limit) {
		 
		 List<StatisData> list = engineerManager.getStatisDataListByMap(params, offset, limit);
		 return list;
	}

	@Override
	public Integer findStatisDataCountByMap(Map<String, Object> params) {
		 
		return engineerManager.findStatisDataCountByMap(params);
	}

	@Override
	public List<StatisData> getStatisCityDataByMap(Map<String, Object> params,
			Integer offset, Integer limit) {
		 List<StatisData> list = engineerManager.getStatisCityDataByMap(params, offset, limit);
		 return list;
	}

	@Override
	public Integer findStatisCityDataCountByMap(Map<String, Object> params) {
		return engineerManager.findStatisCityDataCountByMap(params);
	}

	@Override
	public List<ChinaData> getChinaAllEngineerDataList() {
		long startTime=System.currentTimeMillis();
		Map<String, Object> paMap=new HashMap<String, Object>();
		paMap.put("type", 1);
		List<ChinaData> list = engineerManager.getChinaProvinceDataList(paMap);
		 if(null!=list && list.size()!=0){
			 String provinceCode="";
			 List<StatisData> cityDatas=null;
			 ChinaData china;
			 for(int i=0,k=list.size();i<k;i++){
				 paMap=new HashMap<String, Object>();
				 china=list.get(i);
				 provinceCode=china.getCode();
				 if(StringUtils.isBlank(provinceCode)){
					 continue;
				 }
				 paMap.put("provinceCode", provinceCode);
				 paMap.put("type", 2);
				 cityDatas=engineerManager.getChinaCityDataList(paMap);
				 china.setCityDatas(cityDatas);
			 }
		 }
		 long endTime=System.currentTimeMillis();
		 System.out.println("获取全国城市数据："+(endTime-startTime)+" ms");
		return list;
	}
	
}